package com.jf.identity.service.impl;

import com.jf.identity.dao.visexamid.PaperDao;
import com.jf.identity.entity.ExamBaseParam;
import com.jf.identity.entity.ExamBaseParamValidator;
import com.jf.identity.entity.Paper;
import com.jf.identity.holder.ExamBaseParamHolder;
import com.jf.identity.service.PaperService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.Charset;

/**
 * Created by x on 2018/4/13.
 */
@Service
public class PaperServiceImpl implements PaperService {
    @Resource
    private ExamBaseParamHolder examBaseParamHolder;
    @Resource
    private PaperDao paperDao;
    @Resource
    private ExamBaseParamValidator examBaseParamValidator;
    @Override
    public boolean insertSparePaperInfo(Paper paper) {
        ExamBaseParam examBaseParam = ExamBaseParam.copyOf(paper);
        ExamBaseParam.isBlank(examBaseParam);
        //对考场的机构代码、考试计划编号、考试场次编号、逻辑考场号做验证
        examBaseParamValidator.validate(examBaseParam,ExamBaseParam.EXAM_ID, ExamBaseParam.EXAM_SESSION_NUM,
                ExamBaseParam.LOGIC_EXAM_ROOM_NUM, ExamBaseParam.ORGCODE);
//        paperDao.deleteSparePaperInfo(examBaseParam);
        //插入要加入uuid
        String uid = getSparePaperUid(examBaseParam);
        paper.setUid(uid);
        int n = paperDao.insertSparePaperInfo(examBaseParam, paper);
        return n != 0;
    }

    private String getSparePaperUid(ExamBaseParam examBaseParam) {
        String md5 = examBaseParam.getExamId() + examBaseParam.getExamSessionNum()
                + examBaseParam.getOrgCode() + examBaseParam.getLogicExamRoomNum();
        return DigestUtils.md5DigestAsHex(md5.getBytes(Charset.forName("UTF-8")));
    }
}
